package com.gaofei;

import cn.hutool.bloomfilter.BitMapBloomFilter;
import com.bw.itext.pdf.service.PdfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@RestController
public class PdfApp {

    public static void main(String[] args) {
        SpringApplication.run(PdfApp.class);
    }

    @Autowired
    PdfService pdfService;
//    @Autowired
//    private RedisTemplate redisTemplate;


    //load data from redis
    @PostConstruct
    public void initKey(){

    }


    @RequestMapping("list")
    public Object list(String userKey) throws IOException {
        ///////////////////初始化布隆过滤器数据//////////////////////
        int capacity = 100;
        // 错误比率
        double errorRate = 0.01;
        //创建一个布隆过滤器
        BitMapBloomFilter filter = new BitMapBloomFilter(capacity);
        //把redis中的key放到布隆过滤器中
        filter.add("listkey1");
        filter.add("listkey2");
        filter.add("listkey3");
        boolean contains = filter.contains(userKey);
        if(contains){
            //模拟从数据库查询的数据
            List<House> dbList = new ArrayList<>();
            House house = new House();
            house.setId(1);
            house.setAddress("北京市");
            house.setCommunity("环境优美");
            dbList.add(house);
            House house1 = new House();
            house1.setId(2);
            house1.setAddress("天津市");
            house.setCommunity("环境优美");
            dbList.add(house1);
            //展示列表的时候，首页使用redis 缓存
            //1.从redis中查询有没有数据
//            List<House> redisList =  redisTemplate.opsForList().range(userKey, 0, -1);
//            //2.如果有直接返回,否则从数据库中查询,保存到redis一份返回
//            if(redisList!=null && redisList.size()>0){
//                return redisList;
//            }
//            //从数据库查询出来dbList 保存redis一份  返回
//            redisTemplate.opsForList().rightPushAll(userKey, dbList);
//            redisTemplate.expire(userKey, 10, TimeUnit.MINUTES);
            return dbList;
        }

        return "您访问的key不存在!!!";

    }
    @RequestMapping("pdf")
    public String pdf() throws IOException {
        List<House> list = new ArrayList<>();
        House house = new House();
        house.setId(1);
        house.setAddress("北京市");
        house.setCommunity("环境优美");
        list.add(house);
        House house1 = new House();
        house1.setId(2);
        house1.setAddress("天津市");
        house.setCommunity("环境优美");
        list.add(house);
//        生成pdf
        pdfService.export(list,"abc.pdf","导出demo",House.class);
//        电子签章
        pdfService.sign("house.pdf","house.sign.pdf");

        return "success!!!";
    }
}
